<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间案例</title>
    <style>
        span {
            display: inline-block;
            width: 100px;
            height: 50px;
            text-align: center;
            line-height: 50px;
            background-color: pink;
            font-size: 20px;
        }
    </style>
</head>

<body>
    <span>1</span>
    <span>2</span>
    <span>3</span>
    <button>点击</button>
    <script>
        var span = document.querySelectorAll('span');
        var date1 = +new Date('2022-5-27 19:50:00');
        var btn = document.querySelector('button');

        function timer() {
            var date = +new Date();
            var dateT = (date1 - date) / 1000; //距离时间到还有多少秒
            //获得小时
            var h = parseInt(dateT / 60 / 60 % 24);
            h = h < 10 ? '0' + h : h;
            span[0].innerHTML = h;
            //获得分钟
            var m = parseInt(dateT / 60 % 60);
            m = m < 10 ? '0' + m : m;
            span[1].innerHTML = m;
            // 获得秒数
            var s = parseInt(dateT % 60);
            s = s < 10 ? '0' + s : s;
            span[2].innerHTML = s;
        }
        var times = setInterval(timer, 1000);

        function fn() {
            clearInterval(times);
        }
        //在做一个点击暂停事件----claearInterval();
        btn.addEventListener('click', fn);
    </script>
</body>

</html>